import {routerRedux} from 'dva/router';
import api from '../api';
import {message} from 'antd'

export default {
  namespace : 'performanceAttribution',
  state : {
    lPortId: null,// 产品组合ID 默认值 产品ID排序第一的产品 || query中的 lPortId
    cBenchCode: null,// 业绩基准ID 默认值 沪深300 || query中的 cBenchCode
    timeRange: '2', // 时间段 默认值 最近一月
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname, query}) => {
        // 没有参数时，向后端请求数据，并且改变query
        if (pathname === '/customerService/combinatorialAnalysis/performanceAttribution') {
          dispatch({type: 'init', payload: query})
        }
      })
    }
  },
  effects : {
    * init({
				payload
			}, {call, put,select}) {
        // 初始化
				const state=yield select(state => state.performanceAttribution)
				// 看本次的参数和上次的参数是否一致：
				// 一致的话，不请求，不一致的话，正常请求
				// 定义一个对象用于定义参数
				const newObj={}
				for(var key in payload){
					newObj[key]=state[key]
				}
				if(!Object.getOwnPropertyNames(payload).length||JSON.stringify(payload)!=JSON.stringify(newObj)){
          yield put({
						type:'setState',
						payload,
					})
				}
    },

    * getStockTopGood({
    payload
  }, {call, put, select}) {

  // 获取前5大主要贡献数据

    const query={}
    payload.lPortId&&(query.lPortId=payload.lPortId)
    payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)
    payload.timeRange&&(query.timeRange=payload.timeRange)
    query.type="good"
    query.num=5

    const res = yield call(api.performanceAttribution.getStockTop, query)
    if (res.code === 0) {
      const  stockTopGood= res.data
      stockTopGood.list.map((item,i)=>{
        item.id=i+1
      })
      yield put({type: 'setState', payload: {
        stockTopGood,
        }})
    } else {
      message.info('获取前5大主要贡献数据失败！')
    }
  },

    * getStockTopBad({
      payload
    }, {call, put, select}) {

    // 获取前5大主要拖累数据

      const query={}
      payload.lPortId&&(query.lPortId=payload.lPortId)
      payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)
      payload.timeRange&&(query.timeRange=payload.timeRange)
      query.type="bad"
      query.num=5

      const res = yield call(api.performanceAttribution.getStockTop, query)
      if (res.code === 0) {
        const  stockTopBad= res.data
        stockTopBad.list.map((item,i)=>{
          item.id=i+1
        })
        yield put({type: 'setState', payload: {
          stockTopBad,
          }})
      } else {
        message.info('获取前5大主要拖累数据失败！')
      }
    },
    * getBrinson({
    payload
  }, {call, put, select}) {

  // 获取股票归因数据

    const query={}
    payload.lPortId&&(query.lPortId=payload.lPortId)
    payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)
    payload.timeRange&&(query.timeRange=payload.timeRange)


    const res = yield call(api.performanceAttribution.getBrinson, query)
    if (res.code === 0) {
      const  brinson= res.data
      yield put({type: 'setState', payload: {
        brinson,
        }})
    } else {
      message.info('获取股票归因数据失败！')
    }
  },
   
  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
}
